//
//  LoadingView.m
//  HaoLyy
//
//  Created by jk on 2017/3/28.
//  Copyright © 2017年 jk. All rights reserved.
//

#import "LoadingView.h"

@interface LoadingView ()
@property(nonatomic,strong)CAShapeLayer* shapeLayer;
@end

@implementation LoadingView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor clearColor];
        //        [self creaAnimation];
        
    }
    return self;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 
 */
-(void)creaAnimation{
    //创建背景圆环
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.frame = self.bounds;
    //清空填充色
    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
    //设置画笔颜色 即圆环背景色
    self.shapeLayer.strokeColor =  [UIColor colorWithRed:170/255.0 green:210/255.0 blue:254/255.0 alpha:1].CGColor;
    self.shapeLayer.lineWidth = 2;
    //设置画笔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0) radius:self.frame.size.width/2.0 - 2 startAngle:angle(-90) endAngle:angle(90) clockwise:YES];
    //path 决定layer将被渲染成何种形状
    self.shapeLayer.path = path.CGPath;
    self.shapeLayer.lineCap = kCALineCapRound;
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    CALayer *gradientLayer = [CALayer layer];
    gradientLayer.frame = self.bounds;
    
    CAGradientLayer *gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame = CGRectMake(0, 0, width,  height);
    gradientLayer1.colors = @[(__bridge id)[UIColor whiteColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
    //startPoint和endPoint属性，他们决定了渐变的方向。这两个参数是以单位坐标系进行的定义，所以左上角坐标是{0, 0}，右下角坐标是{1, 1}
    //startPoint和pointEnd 分别指定颜色变换的起始位置和结束位置.
    //当开始和结束的点的x值相同时, 颜色渐变的方向为纵向变化
    //当开始和结束的点的y值相同时, 颜色渐变的方向为横向变化
    //其余的 颜色沿着对角线方向变化
    gradientLayer1.startPoint = CGPointMake(-1, 0);
    gradientLayer1.endPoint = CGPointMake(1, 1);
    [gradientLayer addSublayer:gradientLayer1];
    
    gradientLayer1.mask=self.shapeLayer;
    
    [self.layer addSublayer:gradientLayer];
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 0.8;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = FLT_MAX;
    rotationAnimation.removedOnCompletion=NO;
    [gradientLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    //    [gradientLayer addAnimation:[CABasicAnimation rotation:1 degree:angle(360) direction:1 repeatCount:FLT_MAX] forKey:@"transform"];
    
}
//-(void)drawRect:(CGRect)rect{
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色
//    CGContextSetLineWidth(context, 20);//线的宽度
//    CGFloat radius=self.frame.size.width/2.0 - 10;
//    CGContextAddArc(context, self.frame.size.width/2.0, self.frame.size.width/2.0, radius, 0, M_PI*2, 0); //添加一个圆
//    CGContextDrawPath(context, kCGPathStroke);//绘制填充
//}
@end
